﻿using fuzhu;
using SqlSuarEntity;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using U8COWeb;
using XXX.Bo;
using XXX.Common;
using XXX.Models;
using XXX.Models.AS;
using XXX.Models.XXXEntities;
using ZDEntity;
using Inventory = ZDEntity.Inventory;

namespace Yonyou.U8.BO
{
    /// <summary>
    ///项目档案
    /// </summary>
    public class FitemVM
    {
        //当前的数据库连接字符串
        public string conStr { get; set; }
        public static string cDatabase = "";

        public string model { get; set; }

        public FitemVM()
        {
            //conStr
            conStr = CanShu.conStr;
        }

        /// <summary>
        /// 根据大类编码和项目编码，返回名称
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetName(string citem_class, string cItemCode, out string citem_cname, out string citemname)
        {

            citem_cname = "";
            citemname = "";
            using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = conStr,
                DbType = SqlSugar.DbType.SqlServer,
                IsAutoCloseConnection = true
            }))
            {

                if (!string.IsNullOrEmpty(citem_class) && !string.IsNullOrEmpty(citem_class))
                {
                    var fitemclass = db.Queryable<fitemclass>().Where(x => x.citem_class == citem_class).First();
                    if (fitemclass != null)
                    {
                        var fitemTable = fitemclass.ctable;
                        string sqlitem = $"select citemcode,citemname,bclose from {fitemTable} where citemcode='{cItemCode}'";
                        var fitem = db.SqlQueryable<fitemss97>(sqlitem).First();
                        if (fitem != null)
                        {
                            if (fitem.bclose == true)
                            {
                                return $"Error: 项目编码{cItemCode}已关闭";
                            }
                            else
                            {
                                citemname = fitem.citemname;
                            }
                            citem_cname = fitemclass.citem_name;
                        }
                        else
                        {
                            return $"Error: 项目编码{cItemCode}不存在";
                        }

                    }
                    else
                    {
                        return $"Error: 项目大类{citem_class}不存在";
                    }

                    return "ok";
                }
                else
                {
                    return "项目大类和编码不能为空";
                }
            }
        }

    }
         
        
}
